Skip to content

Automate SQL schema installation in install.js controller#8421

Draft
Copilot wants to merge 3 commits intomasterfrom
copilot/refactor-installation-process
Draft

Automate SQL schema installation in install.js controller#8421
Copilot wants to merge 3 commits intomasterfrom
copilot/refactor-installation-process

Conversation

Copy link
Copy Markdown

Copilot AI commented Oct 28, 2025

The Bhima installation process currently requires manual execution of SQL scripts via shell scripts to build the database schema. This PR bundles SQL schema files with the application and automates their execution through the install.js controller, enabling zero-script deployment.

Implementation

Schema Setup Functions

  • getSQLFiles() - Discovers and sorts .sql files from server/models/ by filename
  • checkSchemaExists() - Queries information_schema to verify if critical tables exist (idempotency)
  • processSQLWithDelimiters() - Parses SQL files containing DELIMITER commands for stored procedures/functions
  • executeSQLFile() - Executes SQL statements sequentially, ignoring duplicate object errors (ER_TABLE_EXISTS_ERROR, ER_SP_ALREADY_EXISTS, etc.)
  • setupDatabaseSchema() - Orchestrates complete schema setup (exported for external use)

Integration Point

Modified proceedInstall() to call setupDatabaseSchema() before creating enterprise/project/user:

exports.proceedInstall = async (req, res) => {
  const { enterprise, project, user } = req.body;

  const isInstalled = await basicInstallExist();
  if (isInstalled) {
    throw new BadRequest('The application is already installed');
  }

  // Setup database schema if it doesn't exist
  await setupDatabaseSchema();

  const location = await defaultEnterpriseLocation();
  await createEnterpriseProjectUser(enterprise, project, user, location.uuid);
  res.redirect('/');
};

Behavior

  • First install: Executes all SQL files in order (01-schema.sql, 02-functions.sql, ..., 99-debug.sql)
  • Existing schema: Detects presence via information_schema query and skips execution
  • Partial failure: Idempotent error codes ignored, other errors logged and re-thrown
  • DELIMITER handling: Correctly parses stored procedures/functions by tracking delimiter state per line

Backward Compatibility

Existing installation process unchanged. If schema was already built via build-database.sh, the existence check detects it and skips SQL execution.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • googlechromelabs.github.io
    • Triggering command: node install.mjs (dns block)
  • https://storage.googleapis.com/chrome-for-testing-public/141.0.7390.78/linux64/chrome-headless-shell-linux64.zip
    • Triggering command: node install.mjs (http block)
  • https://storage.googleapis.com/chrome-for-testing-public/141.0.7390.78/linux64/chrome-linux64.zip
    • Triggering command: node install.mjs (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Feature Request

The Bhima installation process currently requires manual execution of SQL scripts to build the database schema. To simplify and fully automate setup, refactor the application so that all SQL files in server/models/ (e.g., 01-schema.sql, 02-functions.sql, etc.) are bundled with the application. The install.js module should be updated to:

  • Locate all SQL files in server/models/ matching the pattern *.sql.
  • Sort the files in ascending order by filename (e.g., 01-schema.sql, 02-functions.sql, ...).
  • Execute each SQL file against the target database if the required tables/functions/procedures do not exist.
  • Ensure idempotency: only create missing objects, do not overwrite existing data.
  • Use the latest supported NodeJS and MySQL features for file reading and execution.
  • Integrate this logic into the install.js controller so that the application can reliably set up its own schema without requiring a separate install script.

This will enable direct, zero-script installation from the application and streamline deployment in Docker and other automated environments.

Closes #8418.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Refactor installation process for automated database setup Automate SQL schema installation in install.js controller Oct 28, 2025
Copilot AI requested a review from jniles October 28, 2025 21:06
@jniles jniles force-pushed the copilot/refactor-installation-process branch from 1aab6ba to c32ebba Compare October 28, 2025 21:35
Copilot AI and others added 3 commits November 20, 2025 11:04
Co-authored-by: jniles <896472+jniles@users.noreply.github.com>
Co-authored-by: jniles <896472+jniles@users.noreply.github.com>
@jniles jniles force-pushed the copilot/refactor-installation-process branch from c32ebba to 4439f79 Compare November 20, 2025 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Build the BHIMA database during installation process

2 participants